<?php
class Login extends Controller{
	function Login(){
		parent::Controller();
		$this->load->helper('form');
		$this->load->model('User_model');
		//$this->load->library('recaptcha');
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->lang->load('recaptcha');
	}
	function index(){
		$this->load->library('facebook_connect');

		$this->load->model('rememberme_cookies_model');
		//check if already logged
		if ($this->User_model->check_not_remembered_login())
		{
			redirect('profile/index');
		}
		//validation rules
		$validation = array();
		$validation['username'] = array(
	'field' => 'username',
	'label' => 'Username',
	'rules' => 'trim|required|max_length[50]'
	);
	$validation['password'] = array(
        'field' => 'password',
	'label' => 'Password',
	'rules' => 'trim|required|md5'
	);
	/*
	 $validation['remember'] = array(
	 'field' => 'remember',
	 'label' => 'Remember me',
	 'rules' => 'trim|integer]'
	 );
	 */
	$this->form_validation->set_rules($validation);
	$submit_button = $this->input->post('submit');
	if($submit_button !== ""){ // if submit button pressed
		if($this->form_validation->run()){
	  $username = $this->input->post('username',TRUE);
	  $password = $this->input->post('password',TRUE);
	  $user_data = $this->User_model->login($username,$password);
	  if($user_data){
		  if($this->session->userdata('toredirect') != "")
		  {
			  redirect('news/news_visibility');
		  } else {
		  	redirect('profile/index');
		  }
		  exit;
	  }
	  $data['error_message'] = $this->User_model->error_message;
		}
		$data['username'] = $this->input->post('username',TRUE);
	}
	//breadcrum config
	$breadcrumb_list = array(
	array('name'=>'Login', 'href'=>site_url('login')),
	);
	$data['breadcrumb_list'] = $breadcrumb_list;


	$this->load->view(TEMPLATE_NAME.'/front/login',$data);
	}
	function logout()
	{
		$this->load->model('rememberme_cookies_model');
		if ($this->session->userdata('user_id'))
		{
			//check rememberme cookies
			$cookie_data = $this->rememberme_cookies_model->read_cookies();
			if ($cookie_data AND is_array($cookie_data) AND $cookie_data['user_id'] == $this->session->userdata('user_id'))
			{
				//unset rememberme cookies and db data
				$result = $this->rememberme_cookies_model->delete($cookie_data['user_id'], $cookie_data['series']);
			}
		}
		//facebook logout
		$this->load->library('facebook_connect');
		$_COOKIE['fbs_'.$this->config->item('facebook_api_key')] = "";
		if($this->session->userdata('facebook_user')){

			$this->session->unset_userdata('facebook_user');
			//print_r($this->session->userdata('facebook_user'));
			//$this->facebook_connect->client->auth_expireSession();
		}
		//unset session
		$this->User_model->unset_user_session();
		//set variables
		$data['message'] = 'You have been logged out';
		$reload = 'login';
		$data['reload'] = site_url($reload);
		//if ajax request
		redirect($reload);
	}
	function logout_delete()
	{
		$this->load->model('rememberme_cookies_model');
		if ($this->session->userdata('user_id'))
		{
			//check rememberme cookies
			$cookie_data = $this->rememberme_cookies_model->read_cookies();
			if ($cookie_data AND is_array($cookie_data) AND $cookie_data['user_id'] == $this->session->userdata('user_id'))
			{
				//unset rememberme cookies and db data
				$result = $this->rememberme_cookies_model->delete($cookie_data['user_id'], $cookie_data['series']);
			}
		}
		//unset session
		$this->User_model->unset_user_session();
		//set variables
		//if ajax request
		redirect('static_page/success_delete');
	}
	function test(){
		echo $this->session->userdata('username');
	}
	function facebook_connect(){
		$this->load->library('facebook_connect');
		$this->load->config('facebook');
		$_api_key = $this->config->item('facebook_api_key');

		$oauth_token = $_COOKIE['fbs_'.$_api_key];
		$oauth_token = str_replace('"',"",$oauth_token);
		$oauth_url = 'https://graph.facebook.com/me?' .$oauth_token;
		$user_facebook = get_web_page($oauth_url);
		if(!empty($user_facebook)){
			$user_facebook = json_decode($user_facebook['content'],TRUE);
		}
		$oauth_token = ""; // save token yang disini salah, bukan yang ini maksudnya
		//print_r($user_facebook);
		$data['logged_in_facebook'] = 0;
		if(empty($user_facebook['error'])){
			//print_r($user_facebook);
			if(!$this->User_model->check_facebook_user($user_facebook['id'])){
				$this->User_model->force_login_facebook($user_facebook['id']);
				//exit;
				redirect('profile');
			}
			$user_email = $user_facebook['email'];
			$user_first_name = $user_facebook['first_name'];
			$user_last_name = $user_facebook['last_name'];
			$user_name = $user_facebook['name'];
			$user_facebook_id=$user_facebook['id'];
			$submit_button = $this->input->post('submit');
			$user_name = str_replace(" ","_",$user_name);
			$data = array(
				'username'=>$user_name,
				'email'=>$user_email,
			);
			$data['logged_in_facebook'] = 1;
			if($submit_button != ""){
				$validation['username'] = array(
					'field' => 'username',
					'label' => 'Username',
					'rules' => 'trim|required|max_length[50]'
				);
				$validation['password'] = array(
			        'field' => 'password',
					'label' => 'Password',
					'rules' => 'trim|required|md5'
				);
				$this->form_validation->set_rules($validation);
				if($this->form_validation->run() == TRUE){
					$username = $this->input->post('username',TRUE);
					$password = $this->input->post('password',TRUE);
					$user_data = $this->User_model->login($username,$password);
					if($user_data){
						//print_r($user_data);
						$this->User_model->assign_facebook_to_user($user_data['id'],$user_facebook['id']);
						redirect('profile/index');
						exit;
		  			}
	  				$data['error_message'] = $this->User_model->error_message;
				}
			}
		}
		$this->load->view(TEMPLATE_NAME.'/front/login_facebook',$data);
	}
}
/* End of file signup.php */
/* Location: ./system/application/controllers/signup.php */
